package com.geeklan.smart.ui.main.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.geeklan.smart.data.model.Device;
import com.geeklan.smart.databinding.AdapterDeviceListItemBinding;
import com.geeklan.smart.databinding.HomeDeviceItemBinding;
import com.geeklan.smart.ui.device.DeviceInfoActivity;
import com.geeklan.smart.ui.device.VideoCallActivity;

import java.util.List;

public class DeviceListAdapter extends RecyclerView.Adapter<DeviceListAdapter.ViewHolder> {
    private Context mContext;
    private List<Device> mData;

    public DeviceListAdapter(Context context, List<Device> data) {
        this.mData = data;
        this.mContext = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(AdapterDeviceListItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Device device = mData.get(position);
        String deviceId = device.getDeviceId();
        String deviceId_4;
        try {
            deviceId_4 = deviceId.substring(deviceId.length() - 4);
        } catch (Exception e) {
            deviceId_4 = "";
        }
        holder.mBinding.tvDeviceName.setText(device.getDeviceName() + "(" + deviceId_4 + ")");

        holder.mBinding.getRoot().setOnClickListener(v -> {
            DeviceInfoActivity.startActivity(mContext,deviceId,device.getDeviceName(),device.getVideoPreviewImage(),device.getVideoPreviewImage1());
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    // 自定义 ViewHolder
    static class ViewHolder extends RecyclerView.ViewHolder {
        AdapterDeviceListItemBinding mBinding;

        public ViewHolder(@NonNull AdapterDeviceListItemBinding mBinding) {
            super(mBinding.getRoot());
            this.mBinding = mBinding;
        }
    }
}
